const Router = require('koa-router');
const db = require('../database/config/db');
const tableName = 'goods';

const router = new Router();

// 获取商品详情信息的接口
router.post('/getDetailGoodsInfo', async ctx => {
    try {
        let goodsId = ctx.request.body.goodsId
        let result = await db.QUERY_DATA(tableName, {ID: goodsId});
        ctx.body = {
            code: 200,
            message: '请求成功',
            data: result.length > 0 ? result[0] : null
        }
    } catch(err) {
        console.log(err);
        ctx.body = {
            code: 500,
            message: err
        }
    }
        
});

// 获取大类数据的接口
router.get('/getCategoryList', async ctx => {
    try {
        let result = await db.QUERY_DATA('category');
        ctx.body = {
            code: 200,
            message: "请求成功",
            data: result
        }
    } catch (err) {
        ctx.body = {
            code: 500,
            message: err
        }
    }
})

// 直接获取小类数据的接口
router.get('/getSubCategoryList', async ctx => {
    try {
        let result = await db.QUERY_DATA('category_sub');
        ctx.body = {
            code: 200,
            message: "请求成功",
            data: result
        }
    } catch (err) {
        ctx.body = {
            code: 500,
            message: err
        }
    }
})

// 根据类别获取商品列表
router.post('/getGoodsListByCategorySubID', async ctx => {
    try {
        let curSubCateId = ctx.request.body.curSubCateId; // 子类别ID
        let page = ctx.request.body.page; // 当前请求的页数
        let num = 10; // 每页显示数量
        let start = (page - 1) * num; // 开始位置
        // let categorySubId = '2c9f6c946016ea9b016016f79c8e0000';
        let result = await db.QUERY_All(`SELECT * FROM goods WHERE SUB_ID='${curSubCateId}' LIMIT ${start}, ${num}`);
        ctx.body = {
            code: 200,
            message: "请求成功",
            data: result
        }
    } catch (err) {
        ctx.body = {
            code: 500,
            message: err
        }
    }
})

// ====================


module.exports = router;